package com.tornado.community.service;

import com.tornado.community.advice.Constant;
import com.tornado.community.advice.NotificationTypeEnum;
import com.tornado.community.dto.NotificationDto;
import com.tornado.community.mapper.CommentMapper;
import com.tornado.community.mapper.NotificationMapper;
import com.tornado.community.mapper.QuestionMapper;
import com.tornado.community.mapper.UserMapper;
import com.tornado.community.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NotificationService {
    private final CommentMapper commentMapper;
    private final NotificationMapper notificationMapper;
    private final QuestionMapper questionMapper;
    private final UserMapper userMapper;


    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public NotificationService(CommentMapper commentMapper, NotificationMapper notificationMapper, QuestionMapper questionMapper, UserMapper userMapper) {
        this.commentMapper = commentMapper;
        this.notificationMapper = notificationMapper;
        this.questionMapper = questionMapper;
        this.userMapper = userMapper;
    }

    public Notification insertNotification(Comment comment) {
        Notification notification = new Notification();
        notification.setNotifier(comment.getCommenter());
        notification.setGmtCreate(System.currentTimeMillis());
        User user = userMapper.selectByPrimaryKey(comment.getCommenter());
        notification.setNotifierName(user.getName());
        if (Constant.CommentType.QUESTION == comment.getType()) {
            Question question = questionMapper.selectByPrimaryKey(Math.toIntExact(comment.getParentId()));
            notification.setNotifierQuestionId(question.getId());
            notification.setNotifierTitle(question.getTitle());
            notification.setReceiver(question.getCreator());
            notification.setType(Constant.NotificationType.COMMENT_QUESTION);
        } else {
            Comment parentComment = commentMapper.selectByPrimaryKey(comment.getParentId());
            notification.setReceiver(parentComment.getCommenter());
            Question question = questionMapper.selectByPrimaryKey(Math.toIntExact(parentComment.getParentId()));
            notification.setNotifierQuestionId(question.getId());
            notification.setNotifierTitle(question.getTitle());
            notification.setType(Constant.NotificationType.COMMENT_COMMENT);
        }
        if (!notification.getNotifier().equals(notification.getReceiver())){
            notificationMapper.insertSelective(notification);
        }
        return notification;
    }
    public int unreadCount(int receiverId){
        NotificationExample notificationExample = new NotificationExample();
        notificationExample.createCriteria()
                .andReceiverEqualTo(receiverId)
                .andStatusEqualTo(Constant.NotificationStatus.UNREAD);
        return (int) notificationMapper.countByExample(notificationExample);
    }

    public List<NotificationDto> selectNotificationList(int receiverId){
        NotificationExample notificationExample = new NotificationExample();
        notificationExample.createCriteria()
                .andReceiverEqualTo(receiverId);
        notificationExample.setOrderByClause("gmt_create DESC");
        return notificationMapper.selectByExample(notificationExample).stream().map(NotificationDto::new).collect(Collectors.toList());
    }
}
